package geektime.spring.data.simplejdbcdemo;

import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

@SpringBootApplication
@Slf4j
public class SimpleJdbcDemoApplication implements CommandLineRunner {
  @Autowired
  private FooDao fooDao;
  @Autowired
  private BatchFooDao batchFooDao;


  public static void main(String[] args) {
    SpringApplication.run(SimpleJdbcDemoApplication.class, args);
  }

  @Bean
  @Autowired
  public SimpleJdbcInsert simpleJdbcInsert(JdbcTemplate jdbcTemplate) {
    return new SimpleJdbcInsert(jdbcTemplate)
        .withTableName("FOO").usingGeneratedKeyColumns("ID");
  }

  @Bean
  @Autowired
  public NamedParameterJdbcTemplate namedParameterJdbcTemplate(
      @Qualifier("dataSource") DataSource dataSource) {
    log.info("----------------------"+dataSource.toString());
    return new NamedParameterJdbcTemplate(dataSource);
  }

  @Override
  public void run(String... args) throws Exception {
//    fooDao.insertData();
    batchFooDao.batchInsert();
    fooDao.listData();
  }}
